#include <stdio.h>
#include "include/status.h"
#include <Elementary.h>
#include <libcal-character.h>
#include <Edje.h>
#include <Evas.h>
#include <Ecore.h>
#include <Edje_Edit.h>

typedef struct appdata{
	Evas_Object* win_main;
	Evas_Object* layout_main;
	Evas* evas;
	void* char_db_object;

	int id;
} appdata;

static void
win_del(void *data, Evas_Object *obj, void *event_info){
	elm_exit();
}

Evas_Object* load_edj(Evas_Object* win_obj, char* filepath, char* groupname){
	Evas_Object* ret;
	ret=elm_layout_add(win_obj);
	elm_layout_file_set(ret, filepath, groupname);
	elm_win_resize_object_add(win_obj, ret);
	return ret;
}

int status_setting(void *data){
	appdata* ad = (appdata*) data;

	ad->layout_main = load_edj(ad->win_main, "./status.edj", "main");
	Evas_Object* edje = elm_layout_edje_get(ad->layout_main);
	evas_object_show(ad->layout_main);

	GString* ins = character_get_name(ad->char_db_object, ad->id);
	edje_edit_state_text_set(edje, "name", "default 0", ins->str);
	g_string_free(ins, TRUE);

	ins = character_get_class(ad->char_db_object, ad->id);
	edje_edit_state_text_set(edje, "class", "default 0", ins->str);
	g_string_free(ins, TRUE);

	int ins2 = character_get_age(ad->char_db_object, ad->id);
	char x[100];
	sprintf(x, "%d", ins2);
	edje_edit_state_text_set(edje, "age", "default 0", x);

	return 0;
}

EAPI int 
elm_main(int argc, char** argv){
	appdata ad;

	edje_init();
	ecore_evas_init();

	ad.win_main = elm_win_add(NULL, "status", ELM_WIN_BASIC);
	evas_object_resize(ad.win_main,500, 500);
	evas_object_smart_callback_add(ad.win_main, "delete,request", win_del, NULL);
	evas_object_show(ad.win_main);
	ad.char_db_object = libcal_create_object();



	void* object;
	int id;

	if(argc == 2){
		sscanf(argv[1], "%d", &id);
		ad.id = id;
		/*
		object = libcal_create_object();
		GString* ins = character_get_name(object, id);
		printf("%s\n", ins->str);
		g_string_free(ins, TRUE);

		ins = character_get_class(object, id);
		printf("%s\n", ins->str);
		g_string_free(ins, TRUE);

		int ins2 = character_get_age(object, id);
		printf("%d\n", ins2);

		libcal_destroy_object(object);
		*/
		ecore_idler_add(status_setting, &ad);

	}
	elm_run();
	libcal_destroy_object(ad.char_db_object);

	elm_shutdown();

//	Evas* evas = evas_object_evas_get(win_main);
//	evas_font_path_append(evas, "/usr/share/fonts/truetype/freefont/");
//
//	Evas_Object* layout_main;
//	double layout_main_w, layout_main_h;
//	edje_init();
//	ecore_evas_init();
//	layout_main = edje_object_add(evas);
//	int rec = edje_object_file_set(layout_main, "./status_main.edj", "main");
//	printf("ret is %d\n", rec);
//	printf("TRUE is %d\n", TRUE);
//	evas_object_move(layout_main, 0, 0);
//	evas_object_size_hint_min_get(layout_main, &layout_main_w, &layout_main_h);
//	evas_object_resize(layout_main, (int) layout_main_w, (int) layout_main_h);
//	evas_object_show(layout_main);
//	evas_object_resize(win_main, (int) layout_main_w, (int) layout_main_h);
//
//	elm_win_title_set(win_main, "status");
//	evas_object_smart_callback_add(win_main, "delete,request", win_del, NULL);
/*
	Evas_Object* bg = elm_bg_add(win_main);
	evas_object_size_hint_weight_set(bg, 0.0, 0.0);
	elm_win_resize_object_add(win_main, bg);
	evas_object_show(bg);
/*
	Evas_Object* box = elm_box_add(win_main);
	evas_object_size_hint_weight_set(box, 0.0, 0.0);
	elm_win_resize_object_add(win_main, box);
	evas_object_show(box);

	Evas_Object* fr0= elm_frame_add(win_main);
	elm_frame_style_set(fr0, "pad_large");
	elm_box_pack_end(box, fr0);
	evas_object_show(fr0);

	Evas_Object* label = elm_label_add(win_main);
	elm_label_label_set(label, "zedoasdfdsfdsfdsfdsfsdfdsfsdfdsfsfdsfdsful");
	elm_frame_content_set(fr0, label);
	evas_object_show(label);


	Evas_Object* fr1= elm_frame_add(win_main);
	elm_frame_style_set(fr1, "pad_outdent_bottom");
	evas_object_size_hint_align_set(fr1, -1.0, -1.0);
	elm_box_pack_end(box, fr1);
	evas_object_show(fr1);

	Evas_Object* box2 = elm_box_add(win_main);
	evas_object_size_hint_weight_set(box2, 0.0, 0.0);
	elm_win_resize_object_add(win_main, box2);
	evas_object_show(box2);

/*
	Evas_Object* titlebar = elm_label_add(win_main);
	elm_label_label_set(titlebar, "Hello World!<br>asdfdsfsdf<br>");
	evas_object_size_hint_weight_set(titlebar, 1.0, 1.0);
	elm_win_resize_object_add(win_main, titlebar);
	evas_object_show(titlebar);


*/


}
ELM_MAIN()
